﻿using System.Linq.Dynamic;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models;

namespace KPIS.GERP.OMVC.Controllers
{
    public class EmployeeController : Controller
    {
        GERPContext db = new GERPContext(true);

        //
        // GET: /Employee/        

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetPosition(int id)
        {
            var item = (from p in db.Employees
                        where p.EmpId == id
                        select p.Position).FirstOrDefault();
            return Json(item, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetEmployeeForCommandDetail(int id)
        {
            var item = (from p in db.Employees
                        where p.EmpId == id
                        select p).FirstOrDefault();
            return Json(new {
                class_seq = item.ClassId,
                class_name = item.MasClass != null ? item.MasClass.ClassName : null,
                pos_seq = item.PosId,
                pos_ref = item.Position != null ? item.Position.PosNo : null,
                pos_name = item.Position != null ? item.Position.Name : null,
                exec_pos_seq = item.ExecPosId,
                exec_pos_name = item.Position !=null && item.Position.ExecutivePosition != null ? item.Position.ExecutivePosition.Name : null,
                job_site_seq = item.Position != null ? item.Position.JobSiteId : null,
                job_site_name = item.Position.JobSite != null ? item.Position.JobSite.Name : null,
                working_job_site_seq = item.WorkingJobSiteId,
                working_job_site_name = item.JobSite != null ? item.JobSite.Name : null,
                salary = item.Salary
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
